package com.company.planner.web.screens.hotwords;

import com.company.planner.entity.*;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.gui.components.Button;
import com.haulmont.cuba.gui.model.InstanceContainer;
import com.haulmont.cuba.gui.screen.*;

import javax.inject.Inject;

@UiController("planner_HotWords.create")
@UiDescriptor("hot-words-create.xml")
@EditedEntityContainer("hotWordsDc")
@LoadDataBeforeShow
public class HotWordsCreate extends StandardEditor<HotWords> {
    @Inject
    private InstanceContainer<HotWords> hotWordsDc;

    @Inject
    private DataManager dataManager;

    @Subscribe
    public void onInitEntity(InitEntityEvent<HotWords> event) {
        event.getEntity().setSwitchState(SwitchEnum.ON);
    }

    @Subscribe("commitAndCloseBtn")
    public void onCommitAndCloseBtnClick(Button.ClickEvent event) {
        HotWords hotWords = hotWordsDc.getItemOrNull();
//        HotWords words = getEditedEntity();
        if (hotWords != null) {
            HotOperationRecord record = dataManager.create(HotOperationRecord.class);
            record.setNodeState(NodeState.BEGIN);
            record.setOperationState(OperationState.INSERT);
            record.setWord(hotWords);
            record.setWords(hotWords.getWord());
            dataManager.commit(record);
        }
    }

}